package site.yunnong.atvris.service.helper;

import site.yunnong.atvris.dao.domain.ext.VideoDetail;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 用来计算视频评分
 * @author zjh
 * @date 2021/10/7 0:25
 */
public class ScoreHelper {
    
    private static int clickCountScore = 1;
    
    private static int viewCountScore = 2;

    private static int likedCountScore = 3;

    private static int starCountScore = 20;

    private static int commentCountScore = 3;
    
    private static int shareCountScore = 8;

    /**
     * hacker news使用的排行算法
     * r = (p - 1)/(t + 2) ^ 1.8
     * p：得票数，t：时间（小时），
     * p - 1 忽略发起人的投票
     * t + 2 防止最近的帖子导致分母过小
     * @param videoDetail
     * @return
     */
    public static Double calcScoreHackerNews(VideoDetail videoDetail) {
        int w = videoDetail.getClickCount() * clickCountScore + 
                videoDetail.getViewCount() * viewCountScore +
                videoDetail.getLikedCount() * likedCountScore + 
                videoDetail.getStarCount() * starCountScore + 
                videoDetail.getCommentCount() * commentCountScore + 
                videoDetail.getShareCount() * shareCountScore;
        // 得票数
        double p = w;
        Duration duration = Duration.between(videoDetail.getUploadTime(), LocalDateTime.now());
        // 距离上传的时间
        long t = duration.toHours();
        double g = 1.8;
        return p / Math.pow(t + 2, g);
    }
}
